#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

from flask import jsonify, request
from flask_restful import Resource
from utility.odoo_manager import OdooManager  # 操作odoo的单例
from utility.error_code import error_code  # 异常代码
from utility.message_manager import MessageBusManager
from event import event_type
from command import command
import logging
from utility import tools
from command.handler import dict_handler

FORMAT = "%(asctime)s %(thread)d %(message)s"
logging.basicConfig(level=logging.INFO, format=FORMAT)


class TangShaiAddAPI(Resource):
    """
    新增唐筛检查记录
    :request: post
    :param type: 类型  1-早唐筛查 2-中唐筛查
    :param archivesId: 档案ID
    :param ts: 唐筛
            {
               "id": 唐筛Iid
               "fx": 风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "afpJcjg": AFP筛查结果-检查结果
               "afpFxfw": AFP筛查结果-风险范围
               "afpMom": AFP筛查结果-MOM值
               "fxAfp": AFP风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "freeJcjg": free β hCG筛查结果-检查结果
               "freeFxfw": free β hCG筛查结果-风险范围
               "freeMom": free β hCG筛查结果-MOM值
               "fxFree": free β hCG风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "eu3Jcjg": μ E3 筛查结果-检查结果
               "eu3Fxfw": μ E3 筛查结果-风险范围
               "eu3Mom": μ E3 筛查结果-MOM值
               "fxE3u": μ E3风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "st21Jcjg": 21三体风险率筛查结果-检查结果
               "st21Fxfw": 21三体风险率筛查结果-风险范围
               "fx21": 21三体风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "st18Jcjg": 18三体风险率筛查结果-检查结果
               "st18Fxfw": 18三体风险率筛查结果-风险范围
               "fx18": 18三体风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "ntdJcjg": NTD风险筛查结果-检查结果
               "ntdFxfw": NTD风险筛查结果-风险范围
               "ntdMom": NTD风险筛查结果-MOM值
               "fxNtd": NTD风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "jbxxYbbh": 样本编号
               "jbxxCsyz": 超声孕周
               "jbxxCyrq": 采样日期
               "jbxxJcrq": 检查日期
               "jcjgId": 检查机构ID
               "jcjgName": 检查机构名称
               "jzYsId": 接诊医生ID
               "jzYsName": 接诊医生姓名
               "djrId": 登记人ID
               "djrName": 登记人姓名
               "jbxxDjrq": 登记日期
            }
    :returns: json string
    """
    def post(self):
        try:  
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            params['archivesId'] = params.get('archivesId')
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 创建唐筛检查记录
            data_={
                'archives_id': params.get('archivesId'),
                'type': params.get('type'),
                'app_jcjg': tools.nullablea_dict(params.get('ts'), 'afpJcjg'),
                'app_mom': tools.nullablea_dict(params.get('ts'), 'afpMom'),
                'app_fxfw': tools.nullablea_dict(params.get('ts'), 'afpFxfw'),
                'free_jcjg': tools.nullablea_dict(params.get('ts'), 'freeJcjg'),
                'free_mom': tools.nullablea_dict(params.get('ts'), 'freeMom'),
                'free_fxfw': tools.nullablea_dict(params.get('ts'), 'freeFxfw'),
                'e3_jcjg': tools.nullablea_dict(params.get('ts'), 'eu3Jcjg'),
                'e3_mom': tools.nullablea_dict(params.get('ts'), 'eu3Mom'),
                'e3_fxfw': tools.nullablea_dict(params.get('ts'), 'eu3Fxfw'),
                'st21_jcjg': tools.nullablea_dict(params.get('ts'), 'st21Jcjg'),
                'st21_fxfw': tools.nullablea_dict(params.get('ts'), 'st21Fxfw'),
                'st18_jcjg': tools.nullablea_dict(params.get('ts'), 'st18Jcjg'),
                'st18_fxfw': tools.nullablea_dict(params.get('ts'), 'st18Fxfw'),
                'ntd_jcjg': tools.nullablea_dict(params.get('ts'), 'ntdJcjg'),
                'ntd_mom': tools.nullablea_dict(params.get('ts'), 'ntdMom'),
                'ntd_fxfw': tools.nullablea_dict(params.get('ts'), 'ntdFxfw'),
                'jbxx_ybbh': tools.nullablea_dict(params.get('ts'), 'jbxxYbbh'),
                'jbxx_csyz': tools.nullablea_dict(params.get('ts'), 'jbxxCsyz'),
                # 'jbxx_cyrq': tools.nullablea_dict(params.get('ts'), 'jbxxCyrq'),
                # 'jbxx_jcrq': tools.nullablea_dict(params.get('ts'), 'jbxxJcrq'),
                # 'jcjg_id': tools.nullablea_dict(params.get('ts'), 'jcjgId'),
                # 'jcjg_name': tools.nullablea_dict(params.get('ts'), 'jcjgName'),
                # 'jcys_id': tools.nullablea_dict(params.get('ts'), 'jzYsId'),
                # 'jcys_name': tools.nullablea_dict(params.get('ts'), 'jzYsName'),
                # 'djr_id': tools.nullablea_dict(params.get('ts'), 'djrId'),
                # 'djr_name': tools.nullablea_dict(params.get('ts'), 'djrId'),
                # 'jbxx_djrq': tools.nullablea_dict(params.get('ts'), 'jbxxDjrq'),
                'hospital_id': 1
            }
            if tools.nullablea_dict(params.get('ts'), 'jbxxCyrq'):
                data_['jbxx_cyrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxCyrq')
            if tools.nullablea_dict(params.get('ts'), 'jbxxJcrq'):
                data_['jbxx_jcrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxJcrq')
            if tools.nullablea_dict(params.get('ts'), 'jcjgId'):
                data_['jcjg_id'] = tools.nullablea_dict(params.get('ts'), 'jcjgId')
            if tools.nullablea_dict(params.get('ts'), 'jzYsId'):
                data_['jcys_id'] = tools.nullablea_dict(params.get('ts'), 'jzYsId')
            if tools.nullablea_dict(params.get('ts'), 'djrId'):
                data_['djr_id'] = tools.nullablea_dict(params.get('ts'), 'djrId')
            if tools.nullablea_dict(params.get('ts'), 'jbxxDjrq'):
                data_['jbxx_djrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxDjrq')

            data_ = odoo.env['inspection.tang.case'].create(data_)
            create_event = event_type.OperationLogEvent('创建唐筛检查记录', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': data_}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class TangShaiUpdateAPI(Resource):
    """
    修改唐筛检查记录
    :request: put
    :param type: 类型  1-早唐筛查 2-中唐筛查
    :param ts: 唐筛
            {
               "id": 唐筛Iid
               "fx": 风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "afpJcjg": AFP筛查结果-检查结果
               "afpFxfw": AFP筛查结果-风险范围
               "afpMom": AFP筛查结果-MOM值
               "fxAfp": AFP风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "freeJcjg": free β hCG筛查结果-检查结果
               "freeFxfw": free β hCG筛查结果-风险范围
               "freeMom": free β hCG筛查结果-MOM值
               "fxFree": free β hCG风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "eu3Jcjg": μ E3 筛查结果-检查结果
               "eu3Fxfw": μ E3 筛查结果-风险范围
               "eu3Mom": μ E3 筛查结果-MOM值
               "fxE3u": μ E3风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "st21Jcjg": 21三体风险率筛查结果-检查结果
               "st21Fxfw": 21三体风险率筛查结果-风险范围
               "fx21": 21三体风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "st18Jcjg": 18三体风险率筛查结果-检查结果
               "st18Fxfw": 18三体风险率筛查结果-风险范围
               "fx18": 18三体风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "ntdJcjg": NTD风险筛查结果-检查结果
               "ntdFxfw": NTD风险筛查结果-风险范围
               "ntdMom": NTD风险筛查结果-MOM值
               "fxNtd": NTD风险 0 无风险 1 临界风险 2 低风险 3 高风险
               "jbxxYbbh": 样本编号
               "jbxxCsyz": 超声孕周
               "jbxxCyrq": 采样日期
               "jbxxJcrq": 检查日期
               "jcjgId": 检查机构ID
               "jcjgName": 检查机构名称
               "jzYsId": 接诊医生ID
               "jzYsName": 接诊医生姓名
               "djrId": 登记人ID
               "djrName": 登记人姓名
               "jbxxDjrq": 登记日期
            }
    :returns: json string
    """
    def put(self):
        try:
            #  修改唐筛检查记录
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            params['id'] = params.get('ts')['id']
            req_params = ['id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('id', '=', int(params.get('id')))
            ]
            inspection_tang_case = odoo.env['inspection.tang.case'].search(query_)
            if not inspection_tang_case:
                return jsonify({'code': 40005, 'msg': error_code[40005]})      
            data_={
                'type': params.get('type'),
                'app_jcjg': tools.nullablea_dict(params.get('ts'), 'afpJcjg'),
                'app_mom': tools.nullablea_dict(params.get('ts'), 'afpMom'),
                'app_fxfw': tools.nullablea_dict(params.get('ts'), 'afpFxfw'),
                'free_jcjg': tools.nullablea_dict(params.get('ts'), 'freeJcjg'),
                'free_mom': tools.nullablea_dict(params.get('ts'), 'freeMom'),
                'free_fxfw': tools.nullablea_dict(params.get('ts'), 'freeFxfw'),
                'e3_jcjg': tools.nullablea_dict(params.get('ts'), 'eu3Jcjg'),
                'e3_mom': tools.nullablea_dict(params.get('ts'), 'eu3Mom'),
                'e3_fxfw': tools.nullablea_dict(params.get('ts'), 'eu3Fxfw'),
                'st21_jcjg': tools.nullablea_dict(params.get('ts'), 'st21Jcjg'),
                'st21_fxfw': tools.nullablea_dict(params.get('ts'), 'st21Fxfw'),
                'st18_jcjg': tools.nullablea_dict(params.get('ts'), 'st18Jcjg'),
                'st18_fxfw': tools.nullablea_dict(params.get('ts'), 'st18Fxfw'),
                'ntd_jcjg': tools.nullablea_dict(params.get('ts'), 'ntdJcjg'),
                'ntd_mom': tools.nullablea_dict(params.get('ts'), 'ntdMom'),
                'ntd_fxfw': tools.nullablea_dict(params.get('ts'), 'ntdFxfw'),
                'jbxx_ybbh': tools.nullablea_dict(params.get('ts'), 'jbxxYbbh'),
                'jbxx_csyz': tools.nullablea_dict(params.get('ts'), 'jbxxCsyz'),
                # 'jbxx_cyrq': tools.nullablea_dict(params.get('ts'), 'jbxxCyrq'),
                # 'jbxx_jcrq': tools.nullablea_dict(params.get('ts'), 'jbxxJcrq'),
                # 'jcjg_id': tools.nullablea_dict(params.get('ts'), 'jcjgId'),
                # 'jcjg_name': tools.nullablea_dict(params.get('ts'), 'jcjgName'),
                # 'jcys_id': tools.nullablea_dict(params.get('ts'), 'jzYsId'),
                # 'jcys_name': tools.nullablea_dict(params.get('ts'), 'jzYsName'),
                # 'djr_id': tools.nullablea_dict(params.get('ts'), 'djrId'),
                # 'djr_name': tools.nullablea_dict(params.get('ts'), 'djrId'),
                # 'jbxx_djrq': tools.nullablea_dict(params.get('ts'), 'jbxxDjrq'),
            }
            if tools.nullablea_dict(params.get('ts'), 'jbxxCyrq'):
                data_['jbxx_cyrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxCyrq')
            if tools.nullablea_dict(params.get('ts'), 'jbxxJcrq'):
                data_['jbxx_jcrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxJcrq')
            if tools.nullablea_dict(params.get('ts'), 'jcjgId'):
                data_['jcjg_id'] = tools.nullablea_dict(params.get('ts'), 'jcjgId')
            if tools.nullablea_dict(params.get('ts'), 'jzYsId'):
                data_['jcys_id'] = tools.nullablea_dict(params.get('ts'), 'jzYsId')
            if tools.nullablea_dict(params.get('ts'), 'djrId'):
                data_['djr_id'] = tools.nullablea_dict(params.get('ts'), 'djrId')
            if tools.nullablea_dict(params.get('ts'), 'jbxxDjrq'):
                data_['jbxx_djrq'] = tools.nullablea_dict(params.get('ts'), 'jbxxDjrq')

            data_ = odoo.env['inspection.tang.case'].write(int(params.get('id')), data_)
            create_event = event_type.OperationLogEvent('修改唐筛检查记录', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class TangShaiQueryListAPI(Resource):
    """
    查询产前筛查与诊断-唐筛个案登记列表
    :request: get
    :param archivesId: 档案ID
    :param type: NULL 全部 1-早唐筛查 2-中唐筛查
    :returns: json string
    """
    def get(self):
        try:
            #  查询产前筛查与诊断-唐筛个案登记列表
            odoo = OdooManager().get_instance()
            params = request.values
            query_ = []
            if  params.get('archivesId'):
                query_.append(('archives_id', '=', int(params.get('archivesId')))) 
            if  params.get('type'):
                query_.append(('type', '=', int(params.get('type')))) 
            inspection_tang_cases = odoo.env['inspection.tang.case'].search_read(query_)
            data_ = []
            if inspection_tang_cases:
                for tang_case in inspection_tang_cases:
                    data_tmp = {
                        'id': tang_case['id'],
                        'type': tang_case['type'],
                        'fx': '',
                        'afpJcjg': tang_case['app_jcjg'],
                        'afpFxfw': tang_case['app_fxfw'],
                        'afpMom': tang_case['app_mom'],
                        'fxAfp': '',
                        'freeJcjg': tang_case['free_jcjg'],
                        'freeFxfw': tang_case['free_fxfw'],
                        'freeMom': tang_case['free_mom'],
                        'fxFree': '',
                        'eu3Jcjg': tang_case['e3_jcjg'],
                        'eu3Fxfw': tang_case['e3_fxfw'],
                        'eu3Mom': tang_case['e3_mom'],
                        'fxE3u': '',
                        'st21Jcjg': tang_case['st21_jcjg'],
                        'st21Fxfw': tang_case['st21_fxfw'],
                        'fx21': '',
                        'st18Jcjg': tang_case['st18_jcjg'],
                        'st18Fxfw': tang_case['st18_fxfw'],
                        'fx18': '',
                        'ntdJcjg': tang_case['ntd_jcjg'],
                        'ntdFxfw': tang_case['ntd_fxfw'],
                        'ntdMom': tang_case['ntd_mom'],
                        'fxNtd': '',
                        'jbxxYbbh': tang_case['jbxx_ybbh'],
                        'jbxxCsyz': tang_case['jbxx_csyz'],
                        'jbxxCyrq': tang_case['jbxx_cyrq'],
                        'jbxxJcrq': tang_case['jbxx_jcrq'],
                        'jcjgId': tang_case['jcjg_id'][0] if tang_case['jcjg_id'] else None,
                        'jcjgName': tang_case['jcjg_id'][1] if tang_case['jcjg_id'] else None,
                        'jzYsId': tang_case['jcys_id'][0] if tang_case['jcys_id'] else None,
                        'jzYsName': tang_case['jcys_id'][1] if tang_case['jcys_id'] else None,
                        'djrId': tang_case['djr_id'][0] if tang_case['djr_id'] else None,
                        'djrName': tang_case['djr_id'][1] if tang_case['djr_id'] else None,
                        'jbxxDjrq': tang_case['jbxx_djrq']
                    }
                    data_.append(data_tmp)
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class TangShaiGetRecordAPI(Resource):
    """
    唐筛提取实验结果
    :request: get
    :param archivesId: 档案ID
    :param type: NULL 不分类型 1 早唐 2 中唐
    :returns: json string
    """
    def get(self):
        try:
            #  获取孕妇建档信息
            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            if  params.get('type'):
                query_.append(('type', '=', int(params.get('type'))))
            tang_case = odoo.env['inspection.tang.case'].search_read(query_)
            data_ ={}
            if tang_case:
                tang_case = tang_case[0]
                data_ =  {
                    'id': tang_case['id'],
                    'type': tang_case['type'],
                    'fx': '',
                    'afpJcjg': tang_case['app_jcjg'],
                    'afpFxfw': tang_case['app_fxfw'],
                    'afpMom': tang_case['app_mom'],
                    'fxAfp': '',
                    'freeJcjg': tang_case['free_jcjg'],
                    'freeFxfw': tang_case['free_fxfw'],
                    'freeMom': tang_case['free_mom'],
                    'fxFree': '',
                    'eu3Jcjg': tang_case['e3_jcjg'],
                    'eu3Fxfw': tang_case['e3_fxfw'],
                    'eu3Mom': tang_case['e3_mom'],
                    'fxE3u': '',
                    'st21Jcjg': tang_case['st21_jcjg'],
                    'st21Fxfw': tang_case['st21_fxfw'],
                    'fx21': '',
                    'st18Jcjg': tang_case['st18_jcjg'],
                    'st18Fxfw': tang_case['st18_fxfw'],
                    'fx18': '',
                    'ntdJcjg': tang_case['ntd_jcjg'],
                    'ntdFxfw': tang_case['ntd_fxfw'],
                    'ntdMom': tang_case['ntd_mom'],
                    'fxNtd': '',
                    'jbxxYbbh': tang_case['jbxx_ybbh'],
                    'jbxxCsyz': tang_case['jbxx_csyz'],
                    'jbxxCyrq': tang_case['jbxx_cyrq'],
                    'jbxxJcrq': tang_case['jbxx_jcrq'],
                    'jcjgId': tang_case['jcjg_id'],
                    'jcjgName': tang_case['jcjg_name'],
                    'jzYsId': tang_case['jcys_id'][0],
                    'jzYsName': tang_case['jcys_id'][1],
                    'djrId': tang_case['djr_id'][0],
                    'djrName': tang_case['djr_id'][1],
                    'jbxxDjrq': tang_case['jbxx_djrq']
                }
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class TangShaiMakeReportAPI(Resource):
    """
    生成唐筛填写表单
    :request: get
    :param archivesId: 档案ID
    :param type: NULL 不分类型 1 早唐 2 中唐
    :returns: json string
    """
    def get(self):
        try:
            #  获取孕妇建档信息
            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            fields = [
                    'mq_xm','mq_age','mq_mz','mq_zy','mq_csrq',
                    'mq_jt_address','mq_sjhm','fq_sjhm',
                ]
            data_ = {}    
            # 孕产妇档案    
            query_ = [
                    ('id', '=', int(params.get('archivesId')))
                ]
            res_archives = odoo.env['res.archives'].search_read(query_,fields)
            if not res_archives:
                return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
            res_archives_ =  res_archives[0]   
            # 唐筛
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            if  params.get('type'):
                query_.append(('type', '=', int(params.get('type'))))
            tang_case = odoo.env['inspection.tang.case'].search_read(query_)
            if not tang_case:
                return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
            tang_case = tang_case[0]
            # 孕妇初检档案
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            inspection_initial = odoo.env['inspection.initial'].search_read(query_)
            if not inspection_initial:
                return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
            inspection_initial_ = inspection_initial[0] 
            # B超时间
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            inspection_ultrasonics = odoo.env['inspection.ultrasonics'].search_read(query_)
            if not inspection_ultrasonics:
                return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
            inspection_ultrasonics_ = inspection_ultrasonics[0]     
            data_ = {
                'logoImageURL': '',
                'jgName': tang_case['hospital_id'][-1],
                'jbxx': {
                    'yfxm': res_archives_['mq_xm'],
                    'age': res_archives_['mq_age'],
                    'nation': res_archives_['mq_mz'],
                    'mqZy': res_archives_['mq_zy'],
                    'mqCsrq': res_archives_['mq_csrq'],
                    'addr': res_archives_['mq_jt_address'],
                    'mqphone': res_archives_['mq_sjhm'],
                    'fqphone': res_archives_['fq_sjhm']
                },
                'ybqk': {
                    'yjsfgl': dict_handler.get_dict_value(inspection_initial_['yjs_yjbgl'],'ck_ty_sf'),
                    'yjzq': inspection_initial_['yjs_yjzq'],
                    'yc': inspection_initial_['ycs_yc'],
                    'cc': inspection_initial_['ycs_cc'],
                    'mcyj': inspection_initial_['rsqk_mcyjss'],
                    'yz': inspection_initial_['rsqk_jzyz'],
                    'bcsj': inspection_ultrasonics_['jc_date'],
                    'bcNt': dict_handler.get_dict_value(inspection_ultrasonics_['sc_jg'],'ck_ty_wjyc'),
                    'sfdtrs': dict_handler.get_dict_value(inspection_initial_['rsqk_rsteqk'],'ck_rsqk_rsteqk'),
                    'syfs': inspection_initial_['rsqk_ivfet'],
                    'ptzrsj': inspection_initial_['rsqk_ptzrsj'],
                    'sfyxys': dict_handler.get_dict_value(inspection_initial_['grs_xy'],'ck_ty_wy'),
                    'sf21st': tang_case['st21_jcjg'],
                    'sf18st': tang_case['st18_jcjg'],
                    'sfrstyc': dict_handler.get_dict_value(inspection_initial_['jzyc_jzsmx'],'ck_ty_wy'),  # 需要看数据
                    'sfzlza': dict_handler.get_dict_value(inspection_initial_['jzyc_jzsmx'],'ck_ty_wy'), # 需要看数据
                    'qtyc': inspection_initial_['jzyc_jzsmx_text'], # 需要看数据
                    'rshbz': {
                        'jzxgn':dict_handler.get_dict_value(inspection_initial_['jzyc_jzsmx'],'ck_ty_wy'), # 需要看数据
                        'ydsyltnb': dict_handler.get_dict_value(inspection_initial_['ycs_jwrsz_check'],'ck_ycs_jwrsz_check'), # 需要看数据
                        'syyds': inspection_initial_['ycs_jwrsz_radio'] # 需要看数据
                    },
                    'ycrss': {
                        'sfycrss': dict_handler.get_dict_value(inspection_initial_['ycs_ycrss'],'ck_ycs_ycrss'),
                        'yc21': '',#tang_case['id'], 没找到 
                        'yc18': ''#tang_case['id']没找到
                    },
                    'zrlcs': inspection_initial_['ycs_mclc'],
                    'sts': '',#tang_case['id'],没找到 需要看数据
                    'xsesws': '',#tang_case['id'],没找到 需要看数据
                    'yqgrs': '',#tang_case['id'],没找到 需要看数据
                    'qtycls': '',#tang_case['id']没找到 需要看数据
                }
            }
            
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})


class PrenatalScreeningAddAPI(Resource):
    """
    新增产前筛查记录
    :request: post
    :param archivesId: 档案ID
    :param type: 0 全部 1 NT 2 ogtt 3 胎儿缺陷B超 4 高通基因测序
    :param jcjg: 
            {
                "type": 类型
                "jcjbxx": 
                    {
                        "jcysCode": 检查医生code 有可能没有值 只有名称
                        "jcysName": 检查医生姓名
                        "jcrq": 检查日期
                        "jfjgCode": 检查机构ID 有可能没有值 只有名称
                        "jfjgName": 检查机构名称
                        "jzysCode": 就诊医生code 有可能没有值 只有名称
                        "jzysName": 就诊医生姓名
                        "djrCode": 登记人CODE
                        "djrName": 登记人Name
                        "djrq": 登记日期
                    }
                "jgNt": NT结果
                "OGTT": 糖耐OGTT
                    {
                        "kfxt": 空腹血糖
                        "kfxt1": 空腹血糖1小时
                        "kfxt2": 空腹血糖2小时
                        "jg": OGTT结果
                    }
                "teqxjg": 胎儿缺陷B超结果
                "gtjycx": 高通基因测序
            }
    :returns: json string
    """
    def post(self):
        try:  
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId','type']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            record_id = None
            # 创建NT结果记录
            if int(params.get('type')) == 1:
                data_={
                    'archives_id': int(params.get('archivesId')),
                    'sc_jg': params.get('jgNt'),
                    # 'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcysCode'):
                    data_['jc_ys'] = params.get('jcjbxx')['jcysCode']
                record_id = odoo.env['inspection.nt.screen'].create(data_)
                create_event = event_type.OperationLogEvent('创建NT结果记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 创建糖耐OGTT记录
            if int(params.get('type')) == 2:
                data_={
                    'archives_id': params.get('archivesId'),
                    #'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    # 'kg_ptt': params.get('OGTT')['kfxt'],
                    # 'yxs_ptt': params.get('OGTT')['kfxt1'],
                    # 'lxs_ptt': params.get('OGTT')['kfxt2'],
                    'jg': params.get('OGTT')['jg'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt'):
                    data_['kg_ptt'] = params.get('OGTT')['kfxt']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt1'):
                    data_['yxs_ptt'] = params.get('OGTT')['kfxt1']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt2'):
                    data_['lxs_ptt'] = params.get('OGTT')['kfxt2']
                record_id = odoo.env['inspection.sugar.resistance'].create(data_)
                create_event = event_type.OperationLogEvent('创建糖耐OGTT记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 创建胎儿缺陷B超结果记录
            if int(params.get('type')) == 3:
                data_={
                    'archives_id': params.get('archivesId'),
                    'sc_jg': params.get('teqxjg'),
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                record_id = odoo.env['inspection.ultrasonics'].create(data_)
                create_event = event_type.OperationLogEvent('创建胎儿缺陷B超结果记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 创建高通基因测序记录
            if int(params.get('type')) == 4:
                data_={
                    'archives_id': params.get('archivesId'),
                    'sc_jg': params.get('gtjycx'),
                    #'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                record_id = odoo.env['inspection.gene'].create(data_)
                create_event = event_type.OperationLogEvent('创建高通基因测序记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {
                'id': record_id
            }, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class PrenatalScreeningUpdateAPI(Resource):
    """
    修改产前筛查记录
    :request: put
    :param archivesId: 档案ID
    :param type: 0 全部 1 NT 2 ogtt 3 胎儿缺陷B超 4 高通基因测序
    :param jcjg: 
            {
                "type": 类型
                "jcjbxx": 
                    {
                        "jcysCode": 检查医生code 有可能没有值 只有名称
                        "jcysName": 检查医生姓名
                        "jcrq": 检查日期
                        "jfjgCode": 检查机构ID 有可能没有值 只有名称
                        "jfjgName": 检查机构名称
                        "jzysCode": 就诊医生code 有可能没有值 只有名称
                        "jzysName": 就诊医生姓名
                        "djrCode": 登记人CODE
                        "djrName": 登记人Name
                        "djrq": 登记日期
                    }
                "jgNt": NT结果
                "OGTT": 糖耐OGTT
                    {
                        "kfxt": 空腹血糖
                        "kfxt1": 空腹血糖1小时
                        "kfxt2": 空腹血糖2小时
                        "jg": OGTT结果
                    }
                "teqxjg": 胎儿缺陷B超结果
                "gtjycx": 高通基因测序
            }
    :returns: json string
    """
    def put(self):
        try:
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['archivesId','type']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            # 修改NT结果记录
            if int(params.get('type')) == 1:
                inspection_nt_screen = odoo.env['inspection.nt.screen'].search(query_)
                if not inspection_nt_screen:
                    return jsonify({'code': 40005, 'msg': error_code[40005]})  
                data_={
                    'archives_id': params.get('archivesId'),
                    'sc_jg': params.get('jgNt'),
                    # 'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq']
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcysCode'):
                    data_['jc_ys'] = params.get('jcjbxx')['jcysCode']
                data_ = odoo.env['inspection.nt.screen'].write(inspection_nt_screen,data_)
                create_event = event_type.OperationLogEvent('修改NT结果记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 修改糖耐OGTT记录
            if int(params.get('type')) == 2:
                inspection_sugar_resistance = odoo.env['inspection.sugar.resistance'].search(query_)
                if not inspection_sugar_resistance:
                    return jsonify({'code': 40005, 'msg': error_code[40005]})  
                data_={
                    'archives_id': params.get('archivesId'),
                    #'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    # 'kg_ptt': params.get('OGTT')['kfxt'],
                    # 'yxs_ptt': params.get('OGTT')['kfxt1'],
                    # 'lxs_ptt': params.get('OGTT')['kfxt2'],
                    'jg': params.get('OGTT')['jg'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt'):
                    data_['kg_ptt'] = params.get('OGTT')['kfxt']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt1'):
                    data_['yxs_ptt'] = params.get('OGTT')['kfxt1']
                if tools.nullablea_dict(params.get('OGTT'), 'kfxt2'):
                    data_['lxs_ptt'] = params.get('OGTT')['kfxt2']
                data_ = odoo.env['inspection.sugar.resistance'].write(inspection_sugar_resistance,data_)
                create_event = event_type.OperationLogEvent('修改糖耐OGTT记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 修改胎儿缺陷B超结果记录
            if int(params.get('type')) == 3:
                inspection_ultrasonics = odoo.env['inspection.ultrasonics'].search(query_)
                if not inspection_ultrasonics:
                    return jsonify({'code': 40005, 'msg': error_code[40005]}) 
                data_={
                    'archives_id': params.get('archivesId'),
                    'sc_jg': params.get('teqxjg'),
                    #'jc_ys': params.get('jcjbxx')['jcysCode'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                   data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                data_ = odoo.env['inspection.ultrasonics'].write(inspection_ultrasonics,data_)
                create_event = event_type.OperationLogEvent('修改胎儿缺陷B超结果记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件
            # 修改高通基因测序记录
            if int(params.get('type')) == 4:
                inspection_gene = odoo.env['inspection.gene'].search(query_)
                if not inspection_gene:
                    return jsonify({'code': 40005, 'msg': error_code[40005]}) 
                data_={
                    'archives_id': params.get('archivesId'),
                    'sc_jg': params.get('gtjycx'),
                    #'jc_ys': params.get('('jcjbxx')['jcysCo)]'],
                    # 'jc_date': params.get('jcjbxx')['jcrq'],
                    # 'jc_jg': params.get('jcjbxx')['jfjgCode'],
                    # 'jz_ys': params.get('jcjbxx')['jzysCode'],
                    # 'dj_r': params.get('jcjbxx')['djrCode'],
                    # 'dj_date': params.get('jcjbxx')['djrq'],
                    'hospital_id': 1
                }
                if tools.nullablea_dict(params.get('jcjbxx'), 'jcrq'):
                    data_['jc_date'] = params.get('jcjbxx')['jcrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrq'):
                    data_['dj_date'] = params.get('jcjbxx')['djrq']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jfjgCode'):
                    data_['jc_jg'] = params.get('jcjbxx')['jfjgCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'jzysCode'):
                    data_['jz_ys'] = params.get('jcjbxx')['jzysCode']
                if tools.nullablea_dict(params.get('jcjbxx'), 'djrCode'):
                    data_['dj_r'] = params.get('jcjbxx')['djrCode']
                data_ = odoo.env['inspection.gene'].write(inspection_gene,data_)
                create_event = event_type.OperationLogEvent('修改高通基因测序记录', '档案id:{}'.format(params.get('archivesId')))
                message_bus = MessageBusManager().get_message_bus()
                message_bus.handle(create_event)  # 发布事件    
            return jsonify({'code': 0, 'msg': 'success', 'data': {}})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class PrenatalScreeningListAPI(Resource):
    """
    回显产前筛查记录
    :request: get
    :param archivesId: 档案ID
    :param type: 0 全部 1 NT 2 ogtt 3 胎儿缺陷B超 4高通基因测序
    :returns: json string
    """
    def get(self):
        try:
            #  获取孕妇建档信息
            odoo = OdooManager().get_instance()
            params = request.values
            query_ = []
            if params.get('archivesId'):
                query_.append(('archives_id', '=', int(params.get('archivesId'))))
            data_ = []
            data_tmp ={
                'id': '',
                'jcjbxx': {
                    'jcysCode': '',
                    'jcysName': '',
                    'jcrq': '',
                    'jfjgCode': '',
                    'jfjgName': '',
                    'jzysCode': '',
                    'jzysName': '',
                    'djrCode': '',
                    'djrName': '',
                    'djrq': ''
                },
                'type': '',
                'jgNt': '',
                'OGTT': {
                    'kfxt': '',
                    'kfxt1': '',
                    'kfxt2': '',
                    'jg': ''
                },
                'teqxjg': '',
                'gtjycx': ''
            }
            #  NT结果记录
            if  int(params.get('type')) in [0,1]:
                inspection_nt_screen = odoo.env['inspection.nt.screen'].search_read(query_)
                if inspection_nt_screen:
                    screen_ = inspection_nt_screen.pop()
                    data_tmp = json.loads(json.dumps(data_tmp))
                    data_tmp['id'] = screen_['id']
                    data_tmp['jcjbxx']['jcysCode'] = screen_['jc_ys'][0] if screen_['jc_ys'] else None
                    data_tmp['jcjbxx']['jcysName'] = screen_['jc_ys'][1] if screen_['jc_ys'] else None
                    data_tmp['jcjbxx']['jcrq'] = screen_['jc_date']
                    data_tmp['jcjbxx']['jfjgCode'] = screen_['jc_jg'][0] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jfjgName'] = screen_['jc_jg'][1] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jzysCode'] = screen_['jz_ys'][0] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['jzysName'] = screen_['jz_ys'][1] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['djrCode'] = screen_['dj_r'][0] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrName'] = screen_['dj_r'][1] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrq'] = screen_['dj_date']
                    data_tmp['type'] = '1'
                    data_tmp['jgNt'] = screen_['sc_jg']
                    data_.append(data_tmp)
            #  糖耐OGTT记录
            if int(params.get('type')) in [0,2]:
                inspection_sugar_resistance = odoo.env['inspection.sugar.resistance'].search_read(query_)
                if inspection_sugar_resistance:
                    screen_ = inspection_sugar_resistance.pop()
                    data_tmp = json.loads(json.dumps(data_tmp))
                    data_tmp['id'] = screen_['id']
                    data_tmp['jcjbxx']['jcrq'] = screen_['jc_date']
                    data_tmp['jcjbxx']['jfjgCode'] = screen_['jc_jg'][0] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jfjgName'] = screen_['jc_jg'][1] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jzysCode'] = screen_['jz_ys'][0] if screen_['jz_ys'] else ''
                    data_tmp['jcjbxx']['jzysName'] = screen_['jz_ys'][1] if screen_['jz_ys'] else ''
                    data_tmp['jcjbxx']['djrCode'] = screen_['dj_r'][0] if screen_['dj_r'] else ''
                    data_tmp['jcjbxx']['djrName'] = screen_['dj_r'][1] if screen_['dj_r'] else ''
                    data_tmp['jcjbxx']['djrq'] = screen_['dj_date']
                    data_tmp['type'] = '2'
                    data_tmp['OGTT']['kfxt'] = screen_['kg_ptt']
                    data_tmp['OGTT']['kfxt1'] = screen_['yxs_ptt']
                    data_tmp['OGTT']['kfxt2'] = screen_['lxs_ptt']
                    data_tmp['OGTT']['jg'] = screen_['jg']
                    data_.append(data_tmp)
            #  胎儿缺陷B超结果记录
            if int(params.get('type')) in [0,3]:
                inspection_ultrasonics = odoo.env['inspection.ultrasonics'].search_read(query_)
                if inspection_ultrasonics:
                    screen_ = inspection_ultrasonics.pop()
                    data_tmp = json.loads(json.dumps(data_tmp))
                    data_tmp['id'] = screen_['id']
                    data_tmp['jcjbxx']['jcrq'] = screen_['jc_date']
                    data_tmp['jcjbxx']['jfjgCode'] = screen_['jc_jg'][0] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jfjgName'] = screen_['jc_jg'][1] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jzysCode'] = screen_['jz_ys'][0] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['jzysName'] = screen_['jz_ys'][1] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['djrCode'] = screen_['dj_r'][0] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrName'] = screen_['dj_r'][1] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrq'] = screen_['dj_date']
                    data_tmp['type'] = '3'
                    data_tmp['teqxjg'] = screen_['sc_jg']
                    data_.append(data_tmp)
            # 高通基因测序记录
            if int(params.get('type')) in [0,4]:
                inspection_gene = odoo.env['inspection.gene'].search_read(query_)
                if inspection_gene:
                    screen_ = inspection_gene.pop()
                    data_tmp = json.loads(json.dumps(data_tmp))
                    data_tmp['id'] = screen_['id']
                    data_tmp['jcjbxx']['jcrq'] = screen_['jc_date']
                    data_tmp['jcjbxx']['jfjgCode'] = screen_['jc_jg'][0] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jfjgName'] = screen_['jc_jg'][1] if isinstance(screen_['jc_jg'],list) else screen_['jc_jg']
                    data_tmp['jcjbxx']['jzysCode'] = screen_['jz_ys'][0] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['jzysName'] = screen_['jz_ys'][1] if screen_['jz_ys'] else None
                    data_tmp['jcjbxx']['djrCode'] = screen_['dj_r'][0] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrName'] = screen_['dj_r'][1] if screen_['dj_r'] else None
                    data_tmp['jcjbxx']['djrq'] = screen_['dj_date']
                    data_tmp['type'] = '4'
                    data_tmp['gtjycx'] = screen_['sc_jg']
                    data_.append(data_tmp)
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AmyTestAddAPI(Resource):
    """
    新增产前筛查与诊断-艾梅乙检测
    :request: post
    :param archivesId: 档案ID
    :param djrCode: 登记人编码
    :param djrName: 登记人姓名
    :param djrq: 登记日期
    :param Hiv: HIV
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :param md: 梅毒
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :param yg: 乙肝
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :returns: json string
    """
    def post(self):
        try:
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 新增产前筛查与诊断-艾梅乙检测
            data_={
                'archives_id': params.get('archivesId'),
                'hiv_jcjg': params.get('hiv')['jg'],
                'hiv_jcrq': params.get('hiv')['jcrq'],
                'hiv_jcjg_id': params.get('hiv')['jcjgCode'],
                # 'hiv_jcjg_name': params.get('hiv')['jcjgName'],
                'md_jcjg': params.get('md')['jg'],
                'md_jcrq': params.get('md')['jcrq'],
                'md_jcjg_id': params.get('md')['jcjgCode'],
                # 'md_jcjg_name': params.get('md')['jcjgName'],
                'yg_jcjg': params.get('yg')['jg'],
                'yg_jcrq': params.get('yg')['jcrq'],
                'yg_jcjg_id': params.get('yg')['jcjgCode'],
                # 'yg_jcjg_name': params.get('yg')['jcjgName'],
                'register_user': params.get('djrCode'),
                'register_date': params.get('djrq')
            }
            data_ = odoo.env['inspection.amy'].create(data_)
            create_event = event_type.OperationLogEvent('新增产前筛查与诊断-艾梅乙检测', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': data_}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AmyTestUpdateAPI(Resource):
    """
    修改产前筛查与诊断-艾梅乙检测
    :request: put
    :param archivesId: 档案ID
    :param id: 艾梅乙ID
    :param djrCode: 登记人编码
    :param djrName: 登记人姓名
    :param djrq: 登记日期
    :param Hiv: HIV
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :param md: 梅毒
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :param yg: 乙肝
            {
                "jg": 结果
                "jcrq": 检查日期
                "jcjgCode": 检查机构编码
                "jcjgName": 检查机构名称
            }
    :returns: json string
    """
    def put(self):
        try:
            # 修改产前筛查与诊断-艾梅乙检测
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['id']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('id', '=', int(params.get('id')))
            ]      
            inspection_amy = odoo.env['inspection.amy'].search(query_)
            if not inspection_amy:
                return jsonify({'code': 40005, 'msg': error_code[40005]})
            data_={
                # 'id': int(params.get('id')),
                'archives_id': params.get('archivesId'),
                'hiv_jcjg': params.get('hiv')['jg'],
                'hiv_jcrq': params.get('hiv')['jcrq'],
                'hiv_jcjg_id': params.get('hiv')['jcjgCode'],
                # 'hiv_jcjg_name': params.get('Hiv')['jcjgName'],
                'md_jcjg': params.get('md')['jg'],
                'md_jcrq': params.get('md')['jcrq'],
                'md_jcjg_id': params.get('md')['jcjgCode'],
                # 'md_jcjg_name': params.get('md')['jcjgName'],
                'yg_jcjg': params.get('yg')['jg'],
                'yg_jcrq': params.get('yg')['jcrq'],
                'yg_jcjg_id': params.get('yg')['jcjgCode'],
                # 'yg_jcjg_name': params.get('yg')['jcjgName'],
                'register_user': params.get('djrCode'),
                'register_date': params.get('djrq')
            }
            odoo.env['inspection.amy'].write(inspection_amy, data_)
            create_event = event_type.OperationLogEvent('修改产前筛查与诊断-艾梅乙检测', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': params.get('id')}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AmyTestListAPI(Resource):
    """
    回显艾梅乙记录
    :request: get
    :param archivesId: 档案ID
    :returns: json string
    """
    def get(self):
        
        try:
            #  回显艾梅乙记录
            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [('archives_id', '=', int(params.get('archivesId')))]
            inspection_amy = odoo.env['inspection.amy'].search_read(query_)
            data_ = {}
            if inspection_amy:
                inspection_amy = inspection_amy[0]
                data_ = {
                    'id': inspection_amy['id'],
                    'djrCode': inspection_amy['register_user'][0],
                    'djrName': inspection_amy['register_user'][1],
                    'djrq': inspection_amy['register_date'],
                    'hiv': {
                        'jg': inspection_amy['hiv_jcjg'],
                        'jcrq': inspection_amy['hiv_jcrq'],
                        'jcjgCode': inspection_amy['hiv_jcjg_id'][0] if inspection_amy['hiv_jcjg_id'] else None,
                        'jcjgName': inspection_amy['hiv_jcjg_id'][1] if inspection_amy['hiv_jcjg_id'] else None
                    },
                    'md': {
                        'jg': inspection_amy['md_jcjg'],
                        'jcrq': inspection_amy['md_jcrq'],
                        'jcjgCode': inspection_amy['md_jcjg_id'][0] if inspection_amy['md_jcjg_id'] else None,
                        'jcjgName': inspection_amy['md_jcjg_id'][1] if inspection_amy['md_jcjg_id'] else None
                    },
                    'yg': {
                        'jg': inspection_amy['yg_jcjg'],
                        'jcrq': inspection_amy['yg_jcrq'],
                        'jcjgCode': inspection_amy['yg_jcjg_id'][0] if inspection_amy['yg_jcjg_id'] else None,
                        'jcjgName': inspection_amy['yg_jcjg_id'][1] if inspection_amy['yg_jcjg_id'] else None
                    }
                }
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AnemiaTestAddAPI(Resource):
    """
    新增产前筛查与诊断-贫血筛查
    :request: post
    :param archivesId: 档案ID
    :param pxscXss: 血色素
    :param pxscHxq: 红血球
    :param pxscXx: 血型
    :param pxscRh: Rh
    :param pxscZd: 诊断
    :param pxscZl: 治疗
    :param jzysCode: 接诊医生编码
    :param jzysName: 接诊医生姓名
    :param jcjgCode: 检查机构编码
    :param jcjgName: 检查机构名称
    :param jcrq: 检查日期
    :returns: json string
    """
    def post(self):
        try:
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            # 新增产前筛查与诊断-贫血筛查
            data_={
                'archives_id': params.get('archivesId'),
                'hxs': params.get('pxscXss'),
                'hxq': params.get('pxscHxq'),
                'xx': params.get('pxscXx'),
                'rh': params.get('pxscRh'),
                'zd': params.get('pxscZd'),
                'zl': params.get('pxscZl'),
                'jz_ys': params.get('jzysCode'),
                'jc_date': params.get('jcrq')
            }
            data_ = odoo.env['inspection.anemia'].create(data_)
            create_event = event_type.OperationLogEvent('新增产前筛查与诊断-贫血筛查', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': data_}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AnemiaTestUpdateAPI(Resource):
    """
    修改产前筛查与诊断-贫血筛查
    :request: put
    :param archivesId: 档案ID
    :param pxscXss: 血色素
    :param pxscHxq: 红血球
    :param pxscXx: 血型
    :param pxscRh: Rh
    :param pxscZd: 诊断
    :param pxscZl: 治疗
    :param jzysCode: 接诊医生编码
    :param jzysName: 接诊医生姓名
    :param jcjgCode: 检查机构编码
    :param jcjgName: 检查机构名称
    :param jcrq: 检查日期
    :returns: json string
    """
    def put(self):
        try:
            # 修改产前筛查与诊断-贫血筛查
            odoo = OdooManager().get_instance()
            params = request.json
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [
                ('archives_id', '=', int(params.get('archivesId')))
            ]
            inspection_anemia = odoo.env['inspection.anemia'].search(query_)
            if not inspection_anemia:
                return jsonify({'code': 40005, 'msg': error_code[40005]})
            data_={
                'archives_id': params.get('archivesId'),
                'hxs': params.get('pxscXss'),
                'hxq': params.get('pxscHxq'),
                'xx': params.get('pxscXx'),
                'rh': params.get('pxscRh'),
                'zd': params.get('pxscZd'),
                'zl': params.get('pxscZl'),
                'jz_ys': params.get('jzysCode'),
                'jc_date': params.get('jcrq')
            }
            data_ = odoo.env['inspection.anemia'].write(inspection_anemia, data_)
            create_event = event_type.OperationLogEvent('修改产前筛查与诊断-贫血筛查', '档案id:{}'.format(params.get('archivesId')))
            message_bus = MessageBusManager().get_message_bus()
            message_bus.handle(create_event)  # 发布事件
            return jsonify({'code': 0, 'msg': 'success', 'data': {'id': inspection_anemia[0]}, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AnemiaTestListAPI(Resource):
    """
    回显产前筛查与诊断-贫血筛查
    :request: get
    :param archivesId: 档案ID
    :returns: json string
    """
    def get(self):
        try:
            #  回显产前筛查与诊断-贫血筛查
            odoo = OdooManager().get_instance()
            params = request.values
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [('archives_id', '=', int(params.get('archivesId')))]
            inspection_anemia = odoo.env['inspection.anemia'].search_read(query_)
            data_ = {}
            if inspection_anemia:
                inspection_anemia = inspection_anemia.pop()
                data_={
                    'pxscXss': inspection_anemia['hxs'],
                    'pxscHxq': inspection_anemia['hxq'],
                    'pxscXx': inspection_anemia['xx'],
                    'pxscRh': inspection_anemia['rh'],
                    'pxscZd': inspection_anemia['zd'],
                    'pxscZl': inspection_anemia['zl'],
                    'jzysCode': inspection_anemia['jz_ys'][0] if inspection_anemia['jz_ys'] else None,
                    'jzysName': inspection_anemia['jz_ys'][1] if inspection_anemia['jz_ys'] else None,
                    # 'jcjgCode': inspection_anemia['hospital_id'][0],
                    # 'jcjgName': inspection_anemia['hospital_id'][1],
                    'jcrq': inspection_anemia['jc_date']
                }
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})

class AnemiaTestExtResAPI(Resource):
    """
    提取贫血筛查实验结果
    :request: post
    :param archivesId: 档案ID
    :returns: json string
    """
    def post(self):
        try:
            #  提取贫血筛查实验结果
            odoo = OdooManager().get_instance()
            params = tools.get_params(request)
            # 参数校验
            req_params = ['archivesId']
            vrf_res = tools.required_parame_verif(req_params,params)
            if  vrf_res['code'] != 0:
                return jsonify(vrf_res)
            query_ = [('archives_id', '=', int(params.get('archivesId')))]
            inspection_anemia = odoo.env['inspection.anemia'].search_read(query_)
            data_ = {}
            if inspection_anemia:
                inspection_anemia = inspection_anemia[0]
                data_={
                    'pxscXss': inspection_anemia['hxs'],
                    'pxscHxq': inspection_anemia['hxq'],
                    'pxscXx': inspection_anemia['xx'],
                    'pxscRh': inspection_anemia['rh']
                }
            return jsonify({'code': 0, 'msg': 'success', 'data': data_, 'success': True})
        except AttributeError as e:
            logging.exception(e)
            return jsonify({'code': 404, 'msg': error_code[404]})
        except Exception as e:
            logging.exception(e)
            return jsonify({'code': 100, 'msg': error_code[100]})
